﻿using STHPMS1.BLL.B_Core;
using STHPMS1.ICore.I_BLL.Bus;
using STHPMS1.ICore.I_Entity.Info;
using STHPMS1.ICore.I_Entity.Stmpms1;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using ServiceStack.OrmLite;
using STHPMS1.ICore.I_DAL;
using STHPMS1.Common.C_Entity.Stmpms1;
using System.Linq;

using STHPMS1.Common;
using STHPMS1.ICore.I_Serv;
using STHPMS1.ICore;

namespace STHPMS1.BLL.B_Bus
{
    public class E_RoadwaydetailBus : BllBase, IE_RoadwaydetailBill
    {
        public bool ExistsInspcecId(string inspecid)
        {
            ISthpms1Dao dao = this.Scene.Dal.Sthpms1;
            return dao.Exists<E_Roadwaydetail>(q => q.Inspecid == inspecid);
        }
        public T GetSingle<T>(string dy,string rdid,int dir) where T:class , IE_Roadwaydetail
        {
            return this.Scene.Dal.Sthpms1.Single<T>(q=>q.Unit== dy && q.Rdwayid==rdid && q.Direction_Dictid == dir);
        }

        public void Save<T>(T entity)
            where T : class ,IE_Roadwaydetail
        {
            this.Scene.Dal.Sthpms1.Save(entity);
        }

      

        public void Remove(IE_Roadwaydetail entity)
        {
            this.Scene.Dal.Sthpms1.Delete(entity);
        }

       

        public IEnumerable<T> Get<T>(Expression<Func<T, bool>> predicate, IPagingInfo info)
            where T : class, IE_Roadwaydetail
        {
            return this.Scene.Dal.Sthpms1.Select<T>(predicate, info,k=>k.Unit,false);
        }

        public IEnumerable<T> Get<T>(string search, IPagingInfo info)
           where T : class, IE_Roadwaydetail
        {
            return this.Scene.Dal.Sthpms1.E_Roadwaydetail.Get<T>(search,info);
        }


        /// <summary>
        /// 生成评定明细
        /// </summary>
        /// <param name="inspecid">检测ID</param>
        public void Insert(string inspecid)
        {
            //路面
            PCI(inspecid);
            //平整度
            RQI(inspecid);
            //车辙
            RDI(inspecid);
            //抗滑
            SRI(inspecid);
            //结构强度
            PSSI(inspecid);
            //路基
            SCI(inspecid);
            //桥隧构造物
            BCI(inspecid);
            //沿线设施
            TCI(inspecid);

            //单独进行计算PQI，MQI的值
            PQI(inspecid);
            MQI(inspecid);

            ///在路况评定时根据最新检测数据生成模型指标数据
            this.Scene.Bll.Dm_Histperf.CalacModelIndicator(inspecid);

            //生成路段的评定
            //SaveSegEvalu(inspecid);  路段评定表已删除

        }

        /// <summary>
        /// 删除评定明细表
        /// </summary>
        /// <param name="inspecid"></param>
        /// <returns></returns>
        public int Delete(string inspecid)
        {
            return this.Scene.Dal.Sthpms1.DeleteById<IE_Roadwaydetail>(inspecid);
        }

        /// <summary>
        /// 修改单元的检测值
        /// </summary>
        /// <param name="inspecid"></param>
        /// <param name="unit"></param>
        /// <param name="rdwayid"></param>
        /// <param name="colum"></param>
        /// <param name="values"></param>
        public void Update(string inspecid, string unit, string rdwayid, string colum, double values)
        {
            this.Scene.Dal.Sthpms1.E_Roadwaydetail.Update(inspecid, unit, rdwayid, colum, values);
        }

        /// <summary>
        /// E_ROADWAYDETAIL表中获取路线的所有检测ID
        /// </summary>
        /// <returns></returns>
        public List<string> GetInspecid()
        {
            return this.Scene.Dal.Sthpms1.E_Roadwaydetail.GetInspecid();
        }

       
        /// <summary>
        /// 获取评定名称下所有的路线
        /// </summary>
        /// <param name="inspecid"></param>
        /// <returns></returns>
        public List<string> GetRdwayID(string inspecid)
        {
            return this.Scene.Dal.Sthpms1.E_Roadwaydetail.GetRdwayID(inspecid);
        }
         

        /// <summary>
        /// 获取检测评定管理信息
        /// </summary>
        /// <param name="inspecid"></param>
        /// <returns></returns>
        public IS_Inspeaccse GetS_Inspeaccse(string inspecid)
        {
            ISthpms1Dao dao = this.Scene.Dal.Sthpms1;
            return dao.SingleById<S_Inspeaccse>(inspecid);
        }


        #region 检测值的添加和修改
        #region MQI
        /// <summary>
        /// MQI
        /// </summary>
        /// <param name="inspecid"></param>
        /// <param name="unit"></param>
        /// <param name="rdwayid"></param>
        public void MQI(string inspecid, string unit, string rdwayid)
        {
            //计算MQI的值
            E_Roadwaydetail info = this.Scene.Dal.Sthpms1.Single<E_Roadwaydetail>(q => q.Inspecid == inspecid && q.Unit == unit && q.Rdwayid == rdwayid);
            info.Mqi = Common.C_Utils.Calc.MQI(Convert.ToDouble(info.Pqi), Convert.ToDouble(info.Sci), Convert.ToDouble(info.Bci), Convert.ToDouble(info.Tci));
            this.Scene.Dal.Sthpms1.Update<E_Roadwaydetail>(new { MQI = info.Mqi }, q => q.Inspecid == inspecid && q.Unit == unit && q.Rdwayid == rdwayid);
        }

        public void MQI(string inspecid)
        {
            ISthpms1Dao dao = this.Scene.Dal.Sthpms1;
            Expression<Func<E_Roadwaydetail, bool>> exp = null;
            if (inspecid != null && inspecid.ToString().Length > 0)
                exp = pre => pre.Inspecid == inspecid.ToString();
            List<E_Roadwaydetail> list = this.Scene.Bll.E_Roadwaydetail.Get<E_Roadwaydetail>(exp, null).ToList();
            for (int i = 0; i < list.Count; i++)
            {
                E_Roadwaydetail info = list[i];
                if (info.Pqi != null && info.Sci != null && info.Bci != null && info.Tci != null)
                {
                    info.Mqi = Common.C_Utils.Calc.MQI(Convert.ToDouble(info.Pqi), Convert.ToDouble(info.Sci), Convert.ToDouble(info.Bci), Convert.ToDouble(info.Tci));
                    //this.Scene.Dal.Sthpms1.Update<E_Roadwaydetail>(new { MQI = info.Mqi }, q => q.Inspecid == inspecid && q.Unit == info.Unit && q.Rdwayid == info.Rdwayid);
                    dao.Update<E_Roadwaydetail>(new { MQI = info.Mqi }, q => q.Inspecid == inspecid && q.Unit == info.Unit && q.Rdwayid == info.Rdwayid);
                }
            }
        }
        #endregion 

        #region PQI
        public void PQI(string inspecid,string unit,string rdwayid)
        {
            //计算PQI的值

            //获取技术等级
            int Rdclass_Dictid = this.Scene.Bll.B_Invntory.GetRdclass_Dictid(unit);
            E_Roadwaydetail info = this.Scene.Dal.Sthpms1.Single<E_Roadwaydetail>(q => q.Inspecid == inspecid && q.Unit == unit && q.Rdwayid == rdwayid);
            if (Rdclass_Dictid == 160 || Rdclass_Dictid == 161)
            {
                info.Pqi = Common.C_Utils.Calc.PQI(Convert.ToDouble(info.Pci), Convert.ToDouble(info.Rqi), Convert.ToDouble(info.Rdi), Convert.ToDouble(info.Sri));
            }
            else
            {
                //info.Rqi == null ? 0 : Convert.ToDouble(info.Rqi);
                info.Pqi = Common.C_Utils.Calc.PQI(Convert.ToDouble(info.Pci), info.Rqi == null ? 0 : Convert.ToDouble(info.Rqi));
            }

            this.Scene.Dal.Sthpms1.Update<E_Roadwaydetail>(new { PQI = info.Pqi }, q => q.Inspecid == inspecid && q.Unit == unit && q.Rdwayid == rdwayid);

            //更新MQI的值

            //MQI的计算需要：PQI,SCI,BCI,TCI都有检测值才能进行计算
            //MQI(inspecid, unit, rdwayid);
        }

        public void PQI(string inspecid)
        {
            ISthpms1Dao dao = this.Scene.Dal.Sthpms1;
            Expression<Func<E_Roadwaydetail, bool>> exp = null;
            if (inspecid != null && inspecid.ToString().Length > 0)
                exp = pre => pre.Inspecid == inspecid.ToString();
            List<E_Roadwaydetail> list = this.Scene.Bll.E_Roadwaydetail.Get<E_Roadwaydetail>(exp, null).ToList();

            for (int i = 0; i < list.Count; i++)
            {
                E_Roadwaydetail info = list[i];

                int Rdclass_Dictid = this.Scene.Bll.B_Invntory.GetRdclass_Dictid(info.Unit);
                if (Rdclass_Dictid == 160 || Rdclass_Dictid == 161)
                {
                    if (info.Pci != null && info.Rdi != null && info.Rqi != null && info.Sri != null)
                    {
                        info.Pqi = Common.C_Utils.Calc.PQI(Convert.ToDouble(info.Pci), Convert.ToDouble(info.Rqi), Convert.ToDouble(info.Rdi), Convert.ToDouble(info.Sri));
                        //this.Scene.Dal.Sthpms1.Update<E_Roadwaydetail>(new { PQI = info.Pqi }, q => q.Inspecid == inspecid && q.Unit == info.Unit && q.Rdwayid == info.Rdwayid);
                        dao.Update<E_Roadwaydetail>(new { PQI = info.Pqi }, q => q.Inspecid == inspecid && q.Unit == info.Unit && q.Rdwayid == info.Rdwayid);
                    }
                }
                else
                {
                    if (info.Pci != null && info.Rqi != null)
                    {
                        info.Pqi = Common.C_Utils.Calc.PQI(Convert.ToDouble(info.Pci), info.Rqi == null ? 0 : Convert.ToDouble(info.Rqi));
                        //this.Scene.Dal.Sthpms1.Update<E_Roadwaydetail>(new { PQI = info.Pqi }, q => q.Inspecid == inspecid && q.Unit == info.Unit && q.Rdwayid == info.Rdwayid);
                        dao.Update<E_Roadwaydetail>(new { PQI = info.Pqi }, q => q.Inspecid == inspecid && q.Unit == info.Unit && q.Rdwayid == info.Rdwayid);
                    }
                }
            }


        }


        #endregion 

        #region 路面病害-沥青路面 PCI
        /// <summary>
        /// 路面病害-沥青路面
        /// </summary>
        /// <param name="inspecid">检测ID</param>
        public void PCI(string inspecid)
        {
            ISthpms1Dao dao = this.Scene.Dal.Sthpms1;
            IKeyGenrateService serv = MyApp.Locator.GetInstance<IKeyGenrateService>();
            Expression<Func<S_Pmds_Asp, bool>> exp = null;
            if (inspecid != null && inspecid.ToString().Length > 0)
                exp = pre => pre.Inspecid == inspecid.ToString();
            List<S_Pmds_Asp> list = this.Scene.Bll.S_Pmds_Asp.Get<S_Pmds_Asp>(exp, null).ToList();

            for (int i = 0; i < list.Count; i++)
            {
                ///路面病害-沥青路面
                S_Pmds_Asp info = list[i];

                E_Roadwaydetail infoRoadwaydetail = new E_Roadwaydetail();
                infoRoadwaydetail.Id = serv.NewGuid(); //ID
                infoRoadwaydetail.Inspecid = info.Inspecid;
                infoRoadwaydetail.Unit = info.Unit;
                infoRoadwaydetail.Beginmp = dao.Single<B_Unit>(p => p.Unit == info.Unit).Beginmp;
                infoRoadwaydetail.Endmp = dao.Single<B_Unit>(p => p.Unit == info.Unit).Endmp;
                

                infoRoadwaydetail.Year_M = this.Scene.Dal.Sthpms1.SingleById<S_Inspeaccse>(inspecid).Year_M;
                
                infoRoadwaydetail.Rdwayid = info.Rdwayid;

                infoRoadwaydetail.Segid = this.Scene.Bll.B_Invntory.GetSegID(info.Unit);
                //infoRoadwaydetail.RoadnetName = this.Scene.Bll.B_Roadway.GetRoadnetName(info.Rdwayid);
                infoRoadwaydetail.Direction_Dictid = this.Scene.Bll.B_Invntory.GetRirection_Dictid(info.Unit);   //?

                //根据单元ID获取检测长度
                double lenght = this.Scene.Bll.B_Unit.GetLenght(info.Unit);
                infoRoadwaydetail.Pci = Common.C_Utils.Calc.PCI(info,lenght);

                int count = Get<E_Roadwaydetail>(q => q.Inspecid == info.Inspecid && q.Unit == info.Unit && q.Rdwayid == info.Rdwayid, null).Count();
                if (count >= 1)
                {
                    Update(info.Inspecid, info.Unit, info.Rdwayid, "PCI", Convert.ToDouble(infoRoadwaydetail.Pci));
                     
                    //PQI的计算需要：PCI,RQI,RDI,SRI都有检测值才能进行计算
                    //PQI(inspecid, info.Unit, info.Rdwayid);
                }
                else
                {
                    this.Scene.Dal.Sthpms1.Insert<E_Roadwaydetail>(infoRoadwaydetail);
                    ////PQI的计算需要：PCI,RQI,RDI,SRI都有检测值才能进行计算
                    //PQI(inspecid, info.Unit, info.Rdwayid);
                }
            }
        }
        #endregion

        #region 路面平整度 RQI
        /// <summary>
        /// 路面平整度 RQI
        /// </summary>
        /// <param name="inspecid"></param>
        public void RQI(string inspecid)
        {
            ISthpms1Dao dao = this.Scene.Dal.Sthpms1;
            IKeyGenrateService serv = MyApp.Locator.GetInstance<IKeyGenrateService>();
            Expression<Func<S_Rdrgh, bool>> exp = null;
            if (inspecid != null && inspecid.ToString().Length > 0)
                exp = pre => pre.Inspecid == inspecid.ToString();
            List<S_Rdrgh> list = this.Scene.Bll.S_Rdrgh.Get<S_Rdrgh>(exp, null).ToList();

            for (int i = 0; i < list.Count; i++)
            {
                ///路面抗滑检测数据
                S_Rdrgh info = list[i];

                E_Roadwaydetail infoRoadwaydetail = new E_Roadwaydetail();
                infoRoadwaydetail.Id = serv.NewGuid();
                infoRoadwaydetail.Inspecid = info.Inspecid;
                infoRoadwaydetail.Unit = info.Unit;
                infoRoadwaydetail.Beginmp = dao.Single<B_Unit>(p => p.Unit == info.Unit).Beginmp;
                infoRoadwaydetail.Endmp = dao.Single<B_Unit>(p => p.Unit == info.Unit).Endmp;

                infoRoadwaydetail.Year_M = this.Scene.Dal.Sthpms1.SingleById<S_Inspeaccse>(inspecid).Year_M;

                infoRoadwaydetail.Rdwayid = info.Rdwayid;
                infoRoadwaydetail.Segid = this.Scene.Bll.B_Invntory.GetSegID(info.Unit);
                //infoRoadwaydetail.RoadnetName = this.Scene.Bll.B_Roadway.GetRoadnetName(info.Rdwayid);
                infoRoadwaydetail.Direction_Dictid = this.Scene.Bll.B_Invntory.GetRirection_Dictid(info.Unit);

                //根据单元ID获取检测长度
                //double lenght = this.Scene.Bll.B_Unit.GetLenght(info.Unit);

                int Rdclass_Dictid =this.Scene.Bll.B_Invntory.GetRdclass_Dictid(info.Unit);
                if (Rdclass_Dictid == 160 || Rdclass_Dictid == 161)
                {
                    infoRoadwaydetail.Rqi = Common.C_Utils.Calc.RQI1(info);
                }
                else
                {
                    infoRoadwaydetail.Rqi = Common.C_Utils.Calc.RQI2(info);
                }
                

                int count = Get<E_Roadwaydetail>(q => q.Inspecid == info.Inspecid && q.Unit == info.Unit && q.Rdwayid == info.Rdwayid, null).Count();
                if (count >= 1)
                {
                    Update(info.Inspecid, info.Unit, info.Rdwayid, "RQI", Convert.ToDouble(infoRoadwaydetail.Rqi));
                    //PQI的计算需要：PCI,RQI,RDI,SRI都有检测值才能进行计算
                    //PQI(inspecid, info.Unit, info.Rdwayid);
                }
                else
                {

                    Save(infoRoadwaydetail);
                    //PQI的计算需要：PCI,RQI,RDI,SRI都有检测值才能进行计算
                    //PQI(inspecid, info.Unit, info.Rdwayid);
                }
            }
        }
        #endregion 

        #region 路面车辙 RDI
        /// <summary>
        /// 路面车辙 RDI
        /// </summary>
        /// <param name="inspecid"></param>
        public void RDI(string inspecid)
        {
            ISthpms1Dao dao = this.Scene.Dal.Sthpms1;
            IKeyGenrateService serv = MyApp.Locator.GetInstance<IKeyGenrateService>();
            Expression<Func<S_Rutting, bool>> exp = null;
            if (inspecid != null && inspecid.ToString().Length > 0)
                exp = pre => pre.Inspecid == inspecid.ToString();
            List<S_Rutting> list = this.Scene.Bll.S_Rutting.Get<S_Rutting>(exp, null).ToList();

            for (int i = 0; i < list.Count; i++)
            {
                ///路面抗滑检测数据
                S_Rutting info = list[i];

                E_Roadwaydetail infoRoadwaydetail = new E_Roadwaydetail();
                infoRoadwaydetail.Id = serv.NewGuid();
                infoRoadwaydetail.Inspecid = info.Inspecid;
                infoRoadwaydetail.Unit = info.Unit;
                infoRoadwaydetail.Beginmp = dao.Single<B_Unit>(p => p.Unit == info.Unit).Beginmp;
                infoRoadwaydetail.Endmp = dao.Single<B_Unit>(p => p.Unit == info.Unit).Endmp;

                infoRoadwaydetail.Year_M = this.Scene.Dal.Sthpms1.SingleById<S_Inspeaccse>(inspecid).Year_M;

                infoRoadwaydetail.Rdwayid = info.Rdwayid;
                infoRoadwaydetail.Segid = this.Scene.Bll.B_Invntory.GetSegID(info.Unit);
                //infoRoadwaydetail.RoadnetName = this.Scene.Bll.B_Roadway.GetRoadnetName(info.Rdwayid);
                infoRoadwaydetail.Direction_Dictid = this.Scene.Bll.B_Invntory.GetRirection_Dictid(info.Unit);

                infoRoadwaydetail.Rdi = Common.C_Utils.Calc.RDI(Convert.ToDouble(info.Rd));

                int count = Get<E_Roadwaydetail>(q => q.Inspecid == info.Inspecid && q.Unit == info.Unit && q.Rdwayid == info.Rdwayid, null).Count();
                if (count >= 1)
                {
                    Update(info.Inspecid, info.Unit, info.Rdwayid, "RDI", Convert.ToDouble(infoRoadwaydetail.Rdi));
                    //PQI的计算需要：PCI,RQI,RDI,SRI都有检测值才能进行计算
                    //PQI(inspecid, info.Unit, info.Rdwayid);
                }
                else
                {

                    Save(infoRoadwaydetail);
                    //PQI的计算需要：PCI,RQI,RDI,SRI都有检测值才能进行计算
                    //PQI(inspecid, info.Unit, info.Rdwayid);
                }
            }
        }
        #endregion 

        #region 路面结构强度 PSSI
        /// <summary>
        /// 路面结构强度 PSSI
        /// </summary>
        /// <param name="inspecid"></param>
        public void PSSI(string inspecid)
        {
            ISthpms1Dao dao = this.Scene.Dal.Sthpms1;
            IKeyGenrateService serv = MyApp.Locator.GetInstance<IKeyGenrateService>();
            Expression<Func<S_Rdpssi, bool>> exp = null;
            if (inspecid != null && inspecid.ToString().Length > 0)
                exp = pre => pre.Inspecid == inspecid.ToString();
            List<S_Rdpssi> list = this.Scene.Bll.S_Rdpssi.Get<S_Rdpssi>(exp, null).ToList();

            for (int i = 0; i < list.Count; i++)
            {
                ///路面抗滑检测数据
                S_Rdpssi info = list[i];

                E_Roadwaydetail infoRoadwaydetail = new E_Roadwaydetail();
                infoRoadwaydetail.Id = serv.NewGuid();
                infoRoadwaydetail.Inspecid = info.Inspecid;
                infoRoadwaydetail.Unit = info.Unit;
                infoRoadwaydetail.Beginmp = dao.Single<B_Unit>(p => p.Unit == info.Unit).Beginmp;
                infoRoadwaydetail.Endmp = dao.Single<B_Unit>(p => p.Unit == info.Unit).Endmp;

                infoRoadwaydetail.Year_M = this.Scene.Dal.Sthpms1.SingleById<S_Inspeaccse>(inspecid).Year_M;

                infoRoadwaydetail.Rdwayid = info.Rdwayid;
                infoRoadwaydetail.Segid = this.Scene.Bll.B_Invntory.GetSegID(info.Unit);
                //infoRoadwaydetail.RoadnetName = this.Scene.Bll.B_Roadway.GetRoadnetName(info.Rdwayid);
                infoRoadwaydetail.Direction_Dictid = this.Scene.Bll.B_Invntory.GetRirection_Dictid(info.Unit);

                infoRoadwaydetail.Pssi = Common.C_Utils.Calc.PSSI(Convert.ToDouble(info.Ssi));

                int count = Get<E_Roadwaydetail>(q => q.Inspecid == info.Inspecid && q.Unit == info.Unit && q.Rdwayid == info.Rdwayid, null).Count();
                if (count >= 1)
                {
                    Update(info.Inspecid, info.Unit, info.Rdwayid, "PSSI", Convert.ToDouble(infoRoadwaydetail.Pssi));
                }
                else
                {

                    Save(infoRoadwaydetail);
                }
            }
        }
        #endregion 

        #region 路面抗滑 SRI
        /// <summary>
        /// 路面抗滑 SRI
        /// </summary>
        /// <param name="inspecid">检测ID</param>
        public void SRI(string inspecid)
        {
            ISthpms1Dao dao = this.Scene.Dal.Sthpms1;
            IKeyGenrateService serv = MyApp.Locator.GetInstance<IKeyGenrateService>();
            Expression<Func<S_Rdfrc, bool>> exp = null;
            if (inspecid != null && inspecid.ToString().Length > 0)
                exp = pre => pre.Inspecid == inspecid.ToString();
            List<S_Rdfrc> list = this.Scene.Bll.S_Rdfrc.Get<S_Rdfrc>(exp, null).ToList();

            for (int i = 0; i < list.Count; i++)
            {
                ///路面抗滑检测数据
                S_Rdfrc info = list[i];

                E_Roadwaydetail infoRoadwaydetail = new E_Roadwaydetail();
                infoRoadwaydetail.Id = serv.NewGuid();
                infoRoadwaydetail.Inspecid = info.Inspecid;
                infoRoadwaydetail.Unit = info.Unit;
                infoRoadwaydetail.Beginmp = dao.Single<B_Unit>(p => p.Unit == info.Unit).Beginmp;
                infoRoadwaydetail.Endmp = dao.Single<B_Unit>(p => p.Unit == info.Unit).Endmp;

                infoRoadwaydetail.Year_M = this.Scene.Dal.Sthpms1.SingleById<S_Inspeaccse>(inspecid).Year_M;

                infoRoadwaydetail.Rdwayid = info.Rdwayid;
                infoRoadwaydetail.Segid = this.Scene.Bll.B_Invntory.GetSegID(info.Unit);
                //infoRoadwaydetail.RoadnetName = this.Scene.Bll.B_Roadway.GetRoadnetName(info.Rdwayid);
                infoRoadwaydetail.Direction_Dictid = this.Scene.Bll.B_Invntory.GetRirection_Dictid(info.Unit);

                infoRoadwaydetail.Sri = Common.C_Utils.Calc.SRI(Convert.ToDouble(info.Sfc));

                int count =Get<E_Roadwaydetail>(q=>q.Inspecid==info.Inspecid && q.Unit==info.Unit && q.Rdwayid==info.Rdwayid, null).Count();
                if (count >= 1)
                {
                    Update(info.Inspecid, info.Unit, info.Rdwayid, "SRI", Convert.ToDouble(infoRoadwaydetail.Sri));
                    //PQI的计算需要：PCI,RQI,RDI,SRI都有检测值才能进行计算
                    //PQI(inspecid, info.Unit, info.Rdwayid);
                }
                else
                {
                    
                    Save(infoRoadwaydetail);
                    //PQI的计算需要：PCI,RQI,RDI,SRI都有检测值才能进行计算
                    //PQI(inspecid, info.Unit, info.Rdwayid);
                }
            }
        }
        #endregion

        #region  路基SCI
        /// <summary>
        /// 路基 SCI
        /// </summary>
        /// <param name="inspecid"></param>
        public void SCI(string inspecid)
        {
            ISthpms1Dao dao = this.Scene.Dal.Sthpms1;
            IKeyGenrateService serv = MyApp.Locator.GetInstance<IKeyGenrateService>();

            Expression<Func<S_Subgrd, bool>> exp = null;
            if (inspecid != null && inspecid.ToString().Length > 0)
                exp = pre => pre.Inspecid == inspecid.ToString();
            List<S_Subgrd> list = this.Scene.Bll.S_Subgrd.Get<S_Subgrd>(exp, null).ToList();

            for (int i = 0; i < list.Count; i++)
            {
                ///路面抗滑检测数据
                S_Subgrd info = list[i];

                E_Roadwaydetail infoRoadwaydetail = new E_Roadwaydetail();
                infoRoadwaydetail.Id = serv.NewGuid();
                infoRoadwaydetail.Inspecid = info.Inspecid;
                infoRoadwaydetail.Unit = info.Unit;
                infoRoadwaydetail.Beginmp = dao.Single<B_Unit>(p => p.Unit == info.Unit).Beginmp;
                infoRoadwaydetail.Endmp = dao.Single<B_Unit>(p => p.Unit == info.Unit).Endmp;

                infoRoadwaydetail.Year_M = this.Scene.Dal.Sthpms1.SingleById<S_Inspeaccse>(inspecid).Year_M;

                infoRoadwaydetail.Rdwayid = info.Rdwayid;
                infoRoadwaydetail.Segid = this.Scene.Bll.B_Invntory.GetSegID(info.Unit);
                //infoRoadwaydetail.RoadnetName = this.Scene.Bll.B_Roadway.GetRoadnetName(info.Rdwayid);
                infoRoadwaydetail.Direction_Dictid = this.Scene.Bll.B_Invntory.GetRirection_Dictid(info.Unit);

                infoRoadwaydetail.Sci = Common.C_Utils.Calc.SCI(info);

                int count = Get<E_Roadwaydetail>(q => q.Inspecid == info.Inspecid && q.Unit == info.Unit && q.Rdwayid == info.Rdwayid, null).Count();
                if (count >= 1)
                {
                    Update(info.Inspecid, info.Unit, info.Rdwayid, "SCI", Convert.ToDouble(infoRoadwaydetail.Sci));
                }
                else
                {

                    Save(infoRoadwaydetail);
                }
            }
        }
        #endregion 

        #region 桥隧构造物 BCI
        /// <summary>
        /// 桥隧构造物 BCI
        /// </summary>
        /// <param name="inspecid"></param>
        public void BCI(string inspecid)
        {
            ISthpms1Dao dao = this.Scene.Dal.Sthpms1;
            IKeyGenrateService serv = MyApp.Locator.GetInstance<IKeyGenrateService>();
            Expression<Func<S_Btstrut, bool>> exp = null;
            if (inspecid != null && inspecid.ToString().Length > 0)
                exp = pre => pre.Inspecid == inspecid.ToString();
            List<S_Btstrut> list = this.Scene.Bll.S_Btstrut.Get<S_Btstrut>(exp, null).ToList();

            for (int i = 0; i < list.Count; i++)
            {
                S_Btstrut info = list[i];

                E_Roadwaydetail infoRoadwaydetail = new E_Roadwaydetail();
                infoRoadwaydetail.Id = serv.NewGuid();
                infoRoadwaydetail.Inspecid = info.Inspecid;
                infoRoadwaydetail.Unit = info.Unit;
                infoRoadwaydetail.Beginmp = dao.Single<B_Unit>(p => p.Unit == info.Unit).Beginmp;
                infoRoadwaydetail.Endmp = dao.Single<B_Unit>(p => p.Unit == info.Unit).Endmp;

                infoRoadwaydetail.Year_M = this.Scene.Dal.Sthpms1.SingleById<S_Inspeaccse>(inspecid).Year_M;

                infoRoadwaydetail.Rdwayid = info.Rdwayid;
                infoRoadwaydetail.Segid = this.Scene.Bll.B_Invntory.GetSegID(info.Unit);
                //infoRoadwaydetail.RoadnetName = this.Scene.Bll.B_Roadway.GetRoadnetName(info.Rdwayid);

                infoRoadwaydetail.Direction_Dictid = this.Scene.Bll.B_Invntory.GetRirection_Dictid(info.Unit);

                infoRoadwaydetail.Bci = Common.C_Utils.Calc.BCI(info);

                int count = Get<E_Roadwaydetail>(q => q.Inspecid == info.Inspecid && q.Unit == info.Unit && q.Rdwayid == info.Rdwayid, null).Count();
                if (count >= 1)
                {
                    Update(info.Inspecid, info.Unit, info.Rdwayid, "BCI", Convert.ToDouble(infoRoadwaydetail.Bci));
                }
                else
                {

                    Save(infoRoadwaydetail);
                }
            }
        }
        #endregion 

        #region 沿线设施 TCI
        /// <summary>
        /// 沿线设施 TCI
        /// </summary>
        /// <param name="inspecid"></param>
        public void TCI(string inspecid)
        {
            ISthpms1Dao dao = this.Scene.Dal.Sthpms1;
            IKeyGenrateService serv = MyApp.Locator.GetInstance<IKeyGenrateService>();
            Expression<Func<S_Rddf, bool>> exp = null;
            if (inspecid != null && inspecid.ToString().Length > 0)
                exp = pre => pre.Inspecid == inspecid.ToString();
            List<S_Rddf> list = this.Scene.Bll.S_Rddf.Get<S_Rddf>(exp, null).ToList();

            for (int i = 0; i < list.Count; i++)
            {
                ///沿线设施
                S_Rddf info = list[i];

                E_Roadwaydetail infoRoadwaydetail = new E_Roadwaydetail();
                infoRoadwaydetail.Id = serv.NewGuid();
                infoRoadwaydetail.Inspecid = info.Inspecid;
                infoRoadwaydetail.Unit = info.Unit;
                infoRoadwaydetail.Beginmp = dao.Single<B_Unit>(p => p.Unit == info.Unit).Beginmp;
                infoRoadwaydetail.Endmp = dao.Single<B_Unit>(p => p.Unit == info.Unit).Endmp;
                infoRoadwaydetail.Year_M = this.Scene.Dal.Sthpms1.SingleById<S_Inspeaccse>(inspecid).Year_M;
                infoRoadwaydetail.Rdwayid = info.Rdwayid;
                infoRoadwaydetail.Segid = this.Scene.Bll.B_Invntory.GetSegID(info.Unit);
                infoRoadwaydetail.Direction_Dictid = this.Scene.Bll.B_Invntory.GetRirection_Dictid(info.Unit);
                infoRoadwaydetail.Tci = Common.C_Utils.Calc.TCI(info);

                int count = Get<E_Roadwaydetail>(q => q.Inspecid == info.Inspecid && q.Unit == info.Unit && q.Rdwayid == info.Rdwayid, null).Count();
                if (count >= 1)
                {
                    Update(info.Inspecid, info.Unit, info.Rdwayid, "TCI", Convert.ToDouble(infoRoadwaydetail.Tci));
                }
                else
                {

                    Save(infoRoadwaydetail);
                }
            }
        }
        #endregion 

        #endregion


        public System.Data.DataTable GetLatelyValue(string unit)
        {
            return this.Scene.Dal.Sthpms1.E_Roadwaydetail.GetLatelyValue(unit);
        }
    }
}
